package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Admin;
import com.xy.model.Station;
import com.xy.service.AdminLoginService;
import com.xy.service.AdminService;
import com.xy.service.impl.AdminLoginServiceImpl;
import com.xy.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/adminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	  
	  AdminService service = new AdminServiceImpl();
	  
	  if ("add".equals(request.getParameter("status"))) {
	    Admin admin = new Admin();
	    admin.setAccount(request.getParameter("account"));
	    admin.setName(request.getParameter("name"));
	    admin.setPhone(request.getParameter("phone"));
	    admin.setPosition(request.getParameter("position"));
	    admin.setRole(request.getParameter("role"));
	    admin.setPwd(request.getParameter("pwd"));
	    admin.setRemark(request.getParameter("remark"));
	    admin.setCreateDate(new Date());
	    
	    if(service.add(admin))
          request.getRequestDispatcher("/adminServlet?status=list&currentPage=0").forward(request,
              response);
        else {
          System.out.println("error");
          request.getRequestDispatcher("/pages/system/system002/user_add.jsp").forward(request,
              response);
        }
        
        
      } else if ("list".equals(request.getParameter("status"))) {
        int currentPage = 0;
        if (request.getParameter("goalpage") == null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
        } else
          currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

        PageResults pageResults = service.listByPage(currentPage);
        
        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "list");
        request.setAttribute("list", null);

        request.getRequestDispatcher("/pages/system/system002/user_list.jsp").forward(request, response);
        
      } else if ("query".equals(request.getParameter("status"))) {
        int currentPage = 0;
        if (request.getParameter("goalpage") == null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
        } else
          currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;
        
        String role = null;
        String list = null;
        
        if (request.getParameter("list") == null) {
          role = request.getParameter("role") == null ? "%" : request.getParameter("role");
          list = role;
        } else {
          list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          role = list;
        }
        
        PageResults pageResults = service.query(role, currentPage);

        request.setAttribute("pageResults", pageResults);
        request.setAttribute("status", "query");
        request.setAttribute("list", list);

        request.getRequestDispatcher("/pages/system/system002/user_list.jsp").forward(request, response);
        
      } else if ("del".equals(request.getParameter("status"))) {
        int id = Integer.valueOf(request.getParameter("id"));
        String preStatus = request.getParameter("preStatus");
        int currentPage = 0;
        if (request.getParameter("currentPage") != null) {
          currentPage = Integer.valueOf(request.getParameter("currentPage"));
        }
        
        Admin admin = new Admin();
        admin.setId(id);
        
        if("query".equals(preStatus)) {
          String list = null;
          if (request.getParameter("list") == null) {
            list = null;
          }else
            list = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
          
          request.setAttribute("list", list);
          if (service.delete(admin)) {
              request.getRequestDispatcher("/adminServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            } else {
              System.out.println("error");
              request.getRequestDispatcher("/adminServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            }
        }else {
          if (service.delete(admin)) {
              request.getRequestDispatcher("/adminServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            } else {
              System.out.println("error");
              request.getRequestDispatcher("/adminServlet?status="+preStatus+"&currentPage="+currentPage).forward(request,
                  response);
            }
        }
        
      } else if ("up".equals(request.getParameter("status"))) {
        
        int id = Integer.valueOf(request.getParameter("id"));
        Admin admin = service.get(id);
        
        if("person".equals(request.getParameter("page"))) {
          admin.setRole(request.getParameter("role"));
          admin.setPhone(request.getParameter("tel"));
          admin.setRemark(request.getParameter("remark"));
          if(service.update(admin)) {
            request.setAttribute("id", admin.getId());
            request.getRequestDispatcher("/adminServlet?status=detail&page=person").forward(request,
                response);
          }else {
            System.out.println("error");
            request.setAttribute("id", admin.getId());
            request.getRequestDispatcher("/adminServlet?status=detail&page=person").forward(request,
                response);
          }
          
        }else if("pswperson".equals(request.getParameter("page"))){
          String psw = request.getParameter("psw");
          if(!service.isExisits(id, request.getParameter("old_psw"))) {
            request.getRequestDispatcher("/pages/person/person001/psw_modify.jsp").forward(request,
                response);
          }else {
            admin.setPwd(psw);
            if(service.update(admin)) {
              request.setAttribute("id", admin.getId());
              request.getRequestDispatcher("/adminServlet?status=detail&page=person").forward(request,
                  response);
            }else {
              System.out.println("error");
              request.setAttribute("id", admin.getId());
              request.getRequestDispatcher("/pages/person/person001/psw_modify.jsp").forward(request,
                  response);
            }
          }
          
        }else {
          admin.setAccount(request.getParameter("account"));
          admin.setName(request.getParameter("name"));
          admin.setPhone(request.getParameter("phone"));
          admin.setPosition(request.getParameter("position"));
          admin.setRole(request.getParameter("role"));
          admin.setPwd(request.getParameter("pwd"));
          admin.setRemark(request.getParameter("remark"));
          if(service.update(admin)) {
            request.getRequestDispatcher("/adminServlet?status=list&currentPage=0").forward(request,
                response);
          }else {
            System.out.println("error");
            request.getRequestDispatcher("/adminServlet?status=list&currentPage=0").forward(request,
                response);
          }
        }
        
      } else if ("detail".equals(request.getParameter("status"))) {
        
        int id = Integer.valueOf(request.getParameter("id"));
        Admin admin = service.get(id);
        request.setAttribute("admin", admin);
        
        if("person".equals(request.getParameter("page"))) {
          if("up".equals(request.getParameter("pre")))
            request.getRequestDispatcher("/pages/person/person001/user_modify.jsp").forward(request,
                response);
          else
            request.getRequestDispatcher("/pages/person/person001/user_detail.jsp").forward(request,
              response);
        }else {
          if("up".equals(request.getParameter("pre")))
            request.getRequestDispatcher("/pages/system/system002/user_modify.jsp").forward(request,
                response);
          else
            request.getRequestDispatcher("/adminServlet?status=list&currentPage=0").forward(request,
                response);
        }
      }
	}

}
